﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ZenHR.AttData.Collection.AttDataRun;
using ZenHR.AttData.DAL;
using ZenHR.AttData.Collection.DataEntity;
using ZenHR.AttData.DAL.DB3Helper;
using ZenHR.AttData.Collection.TaskQueue;
using System.Configuration;
using System.ServiceProcess;

namespace ZenHR.AttData.Collection
{

   public  class Program
    {
       public static void Main(string[] args)
        {
            string task_id = "";
            if (args.Length > 0)
            {
                try
                {
                    task_id = args[0];
                }
                catch (Exception)
                {
                    task_id = "";
                }
            }

            try
            {
                //检测服务是否注册及执行，如需要手工按窗体模式执行，请先关闭服务后，执行
                if (IsServiceIsExisted("CollectionService"))
                {
                    Console.WriteLine("The service is already installed: collectionService");

                    if (IsServiceStart("CollectionService"))
                    {
                        Console.WriteLine("Found that the service is in startup state. If you want to continue, please close the service first!！");
                    }
                    else
                    {
                        Console.WriteLine("The service is not run: collectionService");

                        Console.WriteLine("Begin execution in this window...... ");
                        TaskRun.Run(task_id);

                        return;
                    }
                }
                else
                {
                    Console.WriteLine("The service is not installed: collectionService！");

                    Console.WriteLine("Begin execution in this window...... ");
                    TaskRun.Run(task_id);

                    return;

                }

                //执行服务本体
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[]
                {
                new CollectionService()
                };
                ServiceBase.Run(ServicesToRun);

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
            }
        }

        /// <summary>
        /// 判断某个Windows服务是否启动
        /// </summary>
        /// <returns></returns>
        public static bool IsServiceStart(string serviceName)
        {
            ServiceController psc = new ServiceController(serviceName);
            bool bStartStatus = false;
            try
            {
                if (!psc.Status.Equals(ServiceControllerStatus.Stopped))
                {
                    bStartStatus = true;
                }

                return bStartStatus;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 检查服务存在的存在性
        /// </summary>
        /// <param name=" NameService ">服务名</param>
        /// <returns>存在返回 true,否则返回 false;</returns>
        public static bool IsServiceIsExisted(string NameService)
        {
            ServiceController[] services = ServiceController.GetServices();
            foreach (ServiceController s in services)
            {
                if (s.ServiceName.ToLower() == NameService.ToLower())
                {
                    return true;
                }
            }
            return false;
        }
    }
}
